
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class KeyWord {
	private String year;
	private String word;
	private int num;
	private Map<String,Integer> wcount = new HashMap<String,Integer>(); //各个词汇的年度出现频次
	private Map<String,HashMap> yw = new HashMap<String,HashMap>();//year和对应的年度词汇集合
	private BufferedReader br = null; //文件读取
	private BufferedWriter bw = null; //文件写入
	
	public void readWord(String path) throws IOException {
		
		int count = 0;//记录读取的行数
		String line = "";
		File infile = new File(path);
		br=new BufferedReader(new InputStreamReader(new FileInputStream(infile),"utf-8"));
		HashMap<String,Integer> twc = new HashMap<String,Integer>();
		int tnum=0;
		String[] wlist;
		String[] fw= {"for","in","the","and","of","a","on","an","to","with","by","-","never","was",".","from","some","…",""}; //���ý�ʹڴ��Լ����ӷ�
		List fwl=Arrays.asList(fw);
		while((line=br.readLine())!=null) {
			count++;
			if(count%2==1) {
				year=line;
			}
			else {
				line=line.replace("(","");
				line=line.replace(",","");
				line=line.replace(")","");
				line=line.replace(".","" );
				line=line.replace("\"","");
				line=line.replace("\'", "");
				line=line.replace(":","");
				line=line.replace("?","");
				line=line.replace("\n","");
				line=line.toLowerCase();
				wlist=line.split(" ");
				if(yw.containsKey(year)) {
				twc = yw.get(year);
				}
				else {
					twc=new HashMap<String,Integer>();
				}
				for(int i=0;i<wlist.length;i++) {
					if(!fwl.contains(wlist[i])) {
						if(twc!=null) {
							if(twc.containsKey(wlist[i])) {
								tnum=twc.get(wlist[i])+1;
							}
							else {
								tnum=1;
							}
							twc.put(wlist[i], tnum);
							tnum=0;
						}
						else {
							tnum=1;
							twc.put(wlist[i],tnum);
						}
					yw.put(year,twc);
					}
				}
			}
		}
		
	}
	public void writeKeyWord() throws IOException {
		String wrpath = "data/keyword.txt";
		File wfile=new File(wrpath);
		bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wfile,true),"utf-8"));
		HashMap<String,Integer> twc = new HashMap<String,Integer>();
		Set set = yw.keySet();
		Object[] year = set.toArray();
		Arrays.sort(year); //根据年份排序
		for(Object key:year) {
			if(key!=null) {
			twc=yw.get(key);
			bw.write(key+"\n");
			}
			if(twc!=null) {
				//根据value(即频次)排序
				List<Map.Entry<String,Integer>> list=new ArrayList<Map.Entry<String, Integer>>(twc.entrySet());
				Collections.sort(list,new Comparator<Map.Entry<String, Integer>>(){
					public int compare(Map.Entry<String, Integer> o1,Map.Entry<String,Integer> o2) {
						return o2.getValue().compareTo(o1.getValue());
					}
				});
				int i=0;
				Iterator<Map.Entry<String, Integer>> it=list.iterator();
				while(it.hasNext()&&i<10) {
					i++;
					Map.Entry<String, Integer> wEntry = it.next();
					bw.write(wEntry.getKey()+":"+wEntry.getValue()+"\t");
				}
			}
			bw.write("\n");
			bw.flush();	
		}
		bw.close();
	}
}
